t
Creating Methods during Runtime

*679*The Prograph development environment and your application run concurrently. You can interrupt your application at any time to add or edit methods, create new classes, and create or edit menus, windows, and window items—just about anything you want to do. 

Most object-oriented languages require you to go through elaborate procedures to perform class and instance alterations. You have to “kill” all instances of a class before changing a class, which is then recompiled. You have to destroy an instance, alter the class definition, and then recreate the instance to have that instance take on new attributes, and so on. Prograph does not slow you down with such distractions. You keep your mind on your programming objectives, not on object and class management. The best way to appreciate the dynamic relationship between Prograph’s editors and your application is to see it in action.

u The blinking insertion bar in the Edit field of the Folks Database window indicates your application is ready for input. Type Johnny.

u Press the Add button. A dialog pops up informing you that the Add method in class Person does not exist. Press Return to continue. An execution window for the new Add method opens.

___________________________________________________________
NOTE: Prograph shows you that it created an empty method with the name Add in class Person. You specified this method in the*168* Click Method field of the Button editor when you created the Add button. Prograph has done some of the work for you: it created a new method with the specified name in the specified class. If you were to open the Methods window of class Person, you would see the Add method icon along with the methods you wrote earlier.

Recall the layered-rings graphic at the beginning of this chapter describing the Prograph Development System. The *51*Application Builder manages menus, windows, and events. Windows and menus are the physical manifestations of a Macintosh user interface. The unseen but critically important third component of a user interface is specification of what these windows and menus do when a user interacts with them using the mouse and keyboard. In Macintosh terms, *555*these interactions are called events.

Most Macintosh languages require you to manage the Macintosh *552* event loop, a portion of an application that repetitively looks for and reacts to user events. Prograph manages the event loop automatically, thereby greatly simplifying application development. You concentrate on what your program does when these events happen. You don’t get bogged down just trying to figure out when events happen. Anthropomorphically speaking, Prograph is saying, “I have detected a button-press event. You indicated that when this button was clicked I should execute*682* the Add method in the Person class. This method does not exist, so I have created a skeletal one for you. If you *491*inspect the three roots of the input bar, you see that the Window object in which this event took place is arriving on the left root, and the Button object pressed is arriving on the middle root. And if you are a low-level Macintosh programmer interested in the contents of the actual event record for this mouse click, I have provided it on the right root.”By opening an editable*143*  case window on the Add method, you can immediately tell Prograph what to do whenever its internal event-loop processing detects a user pressing the Add button in the Folks Database window*1181*.
-----------------------------------------------------------

The roots of the input bar of the newly created Add method come labelled with comments indicating the type of data expected there.

u To actually inspect the objects arriving on those roots, press Return.

*553*The execution highlight moves from the input bar to the output bar. The grayed-out roots of the input bar change into unfilled circles, indicating inspectable data values have passed into them.

u Double-click the left root to open a Value window.

The scrolling-list pane in the upper left corner of the Value *1143*window indicates that a Window object has arrived on the root. The larger right-side scrolling list indicates the values*94* of the attributes of this particular instance of Window. The first item, name, shows that the Window with the click event was the Folks Database window.

u Drag the scroll box of the attribute-list pane to the bottom of its scroll area to reveal the last item in the attribute list. This attribute is item list, which is the list of Window Item instances that are part of the Folks Database window.

u To inspect the contents of item list, double-click the item list triangle-shaped attribute icon to open a second layer of Value window.

Notice, item list contains two Window Item *1061**1174*objects, the Edit Text and Button objects that you created. Before you can assign a name to the new Person object to be created, you need to extract the text attribute-string value from the Edit Text object. *720*

u Your target data value is the name typed into the Edit Text item of your application window. To inspect the Edit Text data value, double-click on the attribute icon of the Edit Text object in the attribute pane of the item list value window.

u Drag the scroll box of the attribute list pane almost to the bottom of its scroll area. Find the text attribute icon. Above the icon you see text has the value "Johnny." This is the data you want to pass to the birth method.

___________________________________________________________
NOTE: Making the Add button work involves two simple steps. First, get the text attribute value from the Edit Text item in the item list of the Folks Database window. Then pass this string to the birth method in class Person.

*721*Recall that birth takes a string input, creates a new Person, and sets the new Person’s name attribute equal to the input string. The Person object is delivered to the output-bar terminal to be passed along for further processing. Before you entered the Application Builder, the conception method was the original source of user interaction to set birth in motion. You now replace the modal dialog of conception with a more Macintosh-like modeless interaction using the Add method triggered by the Add button in the Folks Database window.
-----------------------------------------------------------

u Option-click the Cancel button of the top Value window, Value of List Item 1, to close*1082* all Value windows at once.

u Double-click the dotted background of the Add execution window to open its editable case window.

u Drag the case window down and to the right so the case window does not obscure the execution window. This way you can create some code, activate the execution window, step through the evolving method, and then go back to the case window to develop the method further.

u Select the left root of the case-window input bar. Option-Command-click to place a new operation, and open the ˙Info… dialog.

u Scroll to select the find-item *842*entry. Read its description and then click the Name pop-up. Press Return to transform the generic operation into a find-item operation. The Window object arriving on the left root of the input bar should be feeding into the left terminal of the find-item operation through a datalink.

___________________________________________________________
NOTE: find-item is a powerful primitive that takes a window or menu as input and searches its item list attribute for a particular object. The name attribute of the found object must match the string input to the right terminal of the find-item operation.

In this example, you can quickly reach into the window, find the Edit Text item, and get its text attribute.
-----------------------------------------------------------

u Double-click the right terminal of find-item to create and attach a constant operation. Type Name and press Return. Dismiss the ˙Info… dialog.

u Click anywhere in the execution window to make it the topmost, active window. 

The Add method performs a rollback*948* from the output bar, which was highlighted and pending execution. The name constant operation is highlighted, ready to pass its value to the right terminal of the*375* find-item operation. The Window object is available on the root of the input bar, ready to be passed to find-item.

u Press Return. The Window object and the string, "Name", arrive on the terminals of find-item, which becomes highlighted. Its roots are solid *962*pattern-filled circles indicating that the find-item operation has not yet *357*executed and has not passed along its output.

u Press Return. The execution highlight moves to the output bar, and the find-item roots become hollow circles*958*.

u Double-click the right root of find-item to inspect the output the operation has produced. You see the Edit Text object that you called Name in its Name field in the Edit Text editor.

u Close the Value window and click the editable case window so you can add more to the Add method.

u Click the right root of find-item, and Option-Command-click another operation. Select*390* Get *649**795*from the Opers menu, type text, and press Return.

*722*You now have extracted the value in the Edit Text object’s text attribute. This value, "Johnny", is what you want to name the Person you are about to create. You need only pass this data along to your user-defined method, birth, in class Person.

u Click the right root of the text get operation, and Option-Command-click another operation. Type //birth and press Return.

The Add method now meets its initial design goal of creating a new Person and giving him or her the name from the Edit Text field of the Folks Database window. To confirm this:

u Close the case window of the Add method. Click the Add method execution window to make it active if it is not already. The Add method rolls*949* back its execution to the point at which the text get operation is ready to execute.

u Press Return to execute the text get operation.

u Move the pointer over  the terminal of the //birth operation and click and hold the mouse button down to confirm that the string "Johnny" is ready to flow into the birth operation..  Remember, the single-click and hold technique is the fastest way of inspecting data values.

u Press Return to execute the birth method.

u Double-click the root of the //birth method operation to open a Value window, which confirms that you now have a new Person object named Johnny.

u Press Cancel to close the Value window and press Return to complete execution of the Add method.

A design weakness in your Folks Database window is immediately apparent. The name, Johnny, is still in the Edit Text field. You want to avoid having to delete each name entered, and to give a visual cue that the new Person has been created with the new name. To do this, extend the Add method to clear the text attribute of the name Edit Text item. The easiest way to clear the Edit Text field is to perform a text set operation giving this attribute the empty string, " ", as input.
u Press the Add button again to pop up the execution window. Double-click its background to open the editable case window.

u The Edit Text object is made available on the left root of the text get operation for further processing. Select the left root of the text get operation, and Option-Command-click a new operation.

u Select Set *660**806*from the*980* Opers menu.

u Type text and press Return.

u Double-click the right terminal of the text set operation to attach a Constant operation to this input.

u Type " " (quotation mark quotation mark, with no space in between) and press Return.

u You may want to drag a few operation icons around to avoid overlaps or crossed datalinks.

===========================================================
TIPS: You can drag comments, and toggle hiding/showing them by Command-clicking on the roots or terminals to which they are attached.

Option-arrow-keys will nudge selected Prograph objects one pixel at a time. 
===========================================================

u Close the case window and press Return a few times to walk completely through the Add method. Now when the Add method finishes, the Edit Text field is cleared for the next name.

u Switch context or click on any open editor window to suspend*48**1028* your application and bring up the Prograph Editor menu bar.

u
Select*258* Debug *613*in the Exec menu. The check mark on this item disappears, indicating you have left Debug mode. Your application now runs uninterrupted by execution-window pop-ups.

u
Click the Folks Database window or switch context to continue running your application.

u Create a few new people. The process of adding new people is cleaner and more convenient to the user.

u Switch context and activate*332* Debug by selecting it in the Exec menu, to prepare for your next round of interface enhancement.